import 'package:audioplayers/audioplayers.dart';
import 'package:qq_music/tools/NetWorkController.dart';
import 'package:qq_music/tools/extends/Json/PlayMusic/QQ_music/v_key_parse.dart';
import 'dart:convert' as convert;

AudioPlayer audioPlayer = new AudioPlayer();
VKeyParse vKey;

class Sond {
  ////** Undocumented play
  ///  *
  ///  * @param [String] $songmid 试听id 通过这个参数获取vkey
  ///  * @param [String] $fileName 文件id
  ///  * @param [String] $type 文件类型 ape flac 320Kmp3 mp3 m4a
  ///  * @param [Function] $callBack 回调地址
  ///  * @param [Map<String, dynamic>] $parameter 请求参数
  ///  * @return void
  ///*/
  static void play(String songmid, String fileName, String type, Function callBack,
      {Map<String, dynamic> parameter}) async {
    String getSongName() {
      var fileNameType = '';
      switch (type) {
        case 'ape':
          fileNameType = 'A000';
          break;
        case 'flac':
          fileNameType = 'F000';
          break;
        case '320Kmp3':
          fileNameType = 'M800';
          break;
        case 'mp3':
          fileNameType = 'M500';
          break;
        case 'm4a':
          fileNameType = 'C400';
          break;
        default:
          fileNameType = 'C400';
          type = 'm4a';
      }
      return fileNameType;
    }

    /*  NetWorkController.get('https://u.y.qq.com/cgi-bin/musicu.fcg', (data) {
      print(data);
    }, parameter: {
      /* 'callback': 'getplaysongvkey0016289438363160658', */
      'g_tk': '5381',
      /* 'jsonpCallback': 'getplaysongvkey0016289438363160658', */
      'loginUin': '202184199',
      'hostUin': '202184199',
      'format': 'jsonp',
      'inCharset': 'utf8',
      'outCharset': 'utf-8',
      'notice': '0',
      'platform': 'Mac',
      'needNewCode': '0',
      'data':
          '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"7307242250","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"7307242250","songmid":["$songmid"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}',
    }, responseType: ResponseType.plain);
    print(songmid);
    print('https://isure.stream.qqmusic.qq.com/' +
        getSongName() +
        '$songmid.$type' +
        '?vkey=' +
        vKey.data.items[0].vkey +
        '&guid=6964837424&uin=202184199&fromtag=151'); */

    //播放音乐
    //代码

    // login
    /* NetWorkController.get('http://182.254.34.151/base/fcgi-bin/login', (data) {
      print(data);
    }, parameter: {
      'uid': '1166870842',
      'sid': '201910142042101166870842',
      'cv': '60200',
      'ct': '6',
      'OpenUDID': '84668892142273b824b454b60a103c68b874b943',
      'nettype': '2',
      'eno': '10.15.0',
      'cid': '113',
      'qq': '202184199',
      'uin': '202184199',
      'mt': 'MacBookPro13,1',
      'chid': '1',
      'json': '1',
      'chloginstyle': '0',
      'authst'://这是密码 被加密了
          '00015da46dac007015e50d155c8df1eca2fad2075756de1715beaf4eacc0c960cca401a3013524c5a5a53a38f8fb3cf948f4c93c4f08fe0e71ebabc8ed8b946040ea38fd95efcab6e4d4b47d2a316f8d69c679b0b34e98ca932831f9fea35422bd6dd01810949c4da91184612d7d9aff9c2e16c1d4bca441',
      'auth'://这是密码 被加密了
          '00015da46dac007015e50d155c8df1eca2fad2075756de1715beaf4eacc0c960cca401a3013524c5a5a53a38f8fb3cf948f4c93c4f08fe0e71ebabc8ed8b946040ea38fd95efcab6e4d4b47d2a316f8d69c679b0b34e98ca932831f9fea35422bd6dd01810949c4da91184612d7d9aff9c2e16c1d4bca441',
      'wid': '6679226696781422592',
    });
    return; */
    NetWorkController.get('https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg', (data) {
      print('----------------------------Vkey--------------------------------');
      print(data);
      print('----------------------------Vkey--------------------------------');
      Map<String, dynamic> temp = convert.jsonDecode(data);
      vKey = VKeyParse.fromJson(temp);
    }, parameter: {
      'g_tk': '1160855065',
      /* 'jsonpCallback': 'MusicJsonCallback8283129727027805',//这两个不要添加 不然返回的不是json了 */
      'loginUin': '202184199', //这里是qq
      'hostUin': '0',
      'format': 'json',
      'inCharset': 'utf8',
      'outCharset': 'utf-8¬ice=0',
      'needNewCode': '0',
      /* 'callback': 'MusicJsonCallback8283129727027805',//这两个不要添加 不然返回的不是json了 */
      'uin': '202184199', //这里是qq
      'authst'://这里从 login获取的 auth 也就是密码
          '00015da46dac007015e50d155c8df1eca2fad2075756de1715beaf4eacc0c960cca401a3013524c5a5a53a38f8fb3cf948f4c93c4f08fe0e71ebabc8ed8b946040ea38fd95efcab6e4d4b47d2a316f8d69c679b0b34e98ca932831f9fea35422bd6dd01810949c4da91184612d7d9aff9c2e16c1d4bca441',
      'wid': '6679226696781422592',
      'songmid': '$songmid',
      'filename': getSongName() + '$fileName.$type',
      'songtype': '14',
      'localvip': '2',
      'cid': '205361747',
      'platform': 'Mac',
      'guid': '6964837424',
      'downloadfrom': '0',
    });
    /* print('------------------------------------------------------------');
    print(songmid);//获取vkey的id
    print('------------------------------------------------------------');
    print('------------------------------------------------------------');
    print(fileName);//要获取vkey的文件名
    print('------------------------------------------------------------'); */
    String playUrl = 'https://isure.stream.qqmusic.qq.com/' +
        getSongName() +
        '$fileName.$type' +
        '?vkey=' +
        vKey.data.items[0].vkey +
        '&guid=6964837424&uin=202184199&fromtag=151';
    /* print('https://isure.stream.qqmusic.qq.com/' +
        getSongName() +
        '$fileName.$type' +
        '?vkey=' +
        vKey.data.items[0].vkey +
        '&guid=6964837424&uin=202184199&fromtag=151'); */
    print('--------------------------歌曲信息----------------------------------');
    print(playUrl);
    print('--------------------------歌曲信息----------------------------------');
    int result = await audioPlayer.play(playUrl);
    if (callBack != null) {
      callBack('状态吗$result');
    }
  }

  static void pause(
    //暂停
    Function callBack,
    /* String url, Function callBack, {Map<String, dynamic> parameter} */
  ) async {
    int result = await audioPlayer.pause();
    if (callBack != null) {
      callBack(result);
    }
  }

  static void resume(
    //继续播放
    Function callBack,
    /* String url, Function callBack, {Map<String, dynamic> parameter} */
  ) async {
    int result = await audioPlayer.resume();
    if (callBack != null) {
      callBack(result);
    }
  }

  static void getPlaybackProgress(
    //获取播放进度
    Function callBack,
  ) {
    audioPlayer.onAudioPositionChanged.listen(
      (Duration p) => {
        if (callBack != null)
          {
            callBack(p.inSeconds.toInt()),
          }
      },
    );
  }

  static void totalLength(
    //获取总进度
    Function callBack,
  ) {
    audioPlayer.onDurationChanged.listen(
      (Duration d) {
        if (callBack != null) {
          callBack(d.inSeconds.toInt());
        }
      },
    );
  }

  static void continuePlaying(int position, Function callBack) {
    //跳过播放事件
    var result = audioPlayer.seek(Duration(milliseconds: position));
    if (callBack != null) {
      callBack(result);
    }
  }

  static void stopSond(Function callBack) {
    //停止播放
    var result = audioPlayer.stop();
    if (callBack != null) {
      callBack(result);
    }
  }
}
